package com.yinsin.security;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.yinsin.other.LogHelper;
import com.yinsin.utils.ByteUtils;
import com.yinsin.utils.Constants;

public class AES {
    private static final LogHelper log = LogHelper.getLogger(AES.class);
    private static final String DEF_KEY = "JaVaAEs.DEfaUft_kEy";

    /**
     * 加密
     * 
     * @param content
     *            需要加密的内容
     * @param password
     *            加密密码
     * @return
     */
    public static String encrypt(String content, String password) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(password.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            byte[] byteContent = content.getBytes(Constants.CHARSET_UTF8);
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            return ByteUtils.parseByte2HexStr(result); // 加密
        } catch (NoSuchAlgorithmException e) {
            log.error("AES encrypt is error." + e.getMessage());
        } catch (NoSuchPaddingException e) {
            log.error("AES encrypt is error." + e.getMessage());
        } catch (InvalidKeyException e) {
            log.error("AES encrypt is error." + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            log.error("AES encrypt is error." + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            log.error("AES encrypt is error." + e.getMessage());
        } catch (BadPaddingException e) {
            log.error("AES encrypt is error." + e.getMessage());
        }
        return null;
    }

    /**
     * 解密
     * 
     * @param content
     *            待解密内容
     * @param password
     *            解密密钥
     * @return
     */
    public static String decrypt(String content, String password) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(password.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(ByteUtils.parseHexStr2Byte(content));
            return new String(result); // 加密
        } catch (NoSuchAlgorithmException e) {
            log.error("AES decrypt is error." + e.getMessage());
        } catch (NoSuchPaddingException e) {
            log.error("AES decrypt is error." + e.getMessage());
        } catch (InvalidKeyException e) {
            log.error("AES decrypt is error." + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            log.error("AES decrypt is error." + e.getMessage());
        } catch (BadPaddingException e) {
            log.error("AES decrypt is error." + e.getMessage());
        }
        return null;
    }

    /**
     * 加密
     * 
     * @param content
     *            需要加密的内容
     * @return
     */
    public static String encrypt(String content) {
        return encrypt(content, DEF_KEY);
    }

    /**
     * 解密
     * 
     * @param content
     *            待解密内容
     * @return
     */
    public static String decrypt(String content) {
        return decrypt(content, DEF_KEY);
    }
}
